package admin

import (
	"go-weiqi-system/config"
	"go-weiqi-system/internal/models"
	"go-weiqi-system/internal/utils"

	"github.com/gin-gonic/gin"
)

type SuggestionController struct{}

// Index 建议列表
func (ctrl *SuggestionController) Index(c *gin.Context) {
	var param models.PageParam
	if err := c.ShouldBindQuery(&param); err != nil {
		param.Page = 1
		param.PageSize = 10
	}

	status := c.Query("status")
	keyword := c.Query("keyword")

	var suggestions []models.Suggestion
	var total int64

	db := config.DB.Model(&models.Suggestion{}).Preload("User")

	if status != "" {
		db = db.Where("status = ?", status)
	}
	if keyword != "" {
		db = db.Where("content LIKE ?", "%"+keyword+"%")
	}

	db.Count(&total)
	db.Order("id desc").
		Offset(param.GetOffset()).
		Limit(param.GetPageSize()).
		Find(&suggestions)

	utils.Success(c, models.PageResponse{
		List:     suggestions,
		Total:    total,
		Page:     param.GetPage(),
		PageSize: param.GetPageSize(),
	})
}

// Detail 建议详情
func (ctrl *SuggestionController) Detail(c *gin.Context) {
	id := c.Query("id")
	if id == "" {
		utils.Error(c, "参数错误")
		return
	}

	var suggestion models.Suggestion
	if err := config.DB.Preload("User").First(&suggestion, id).Error; err != nil {
		utils.Error(c, "建议不存在")
		return
	}

	utils.Success(c, suggestion)
}

// Reply 回复建议
func (ctrl *SuggestionController) Reply(c *gin.Context) {
	var req struct {
		ID    uint   `json:"id" binding:"required"`
		Reply string `json:"reply" binding:"required"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		utils.Error(c, "参数错误")
		return
	}

	if err := config.DB.Model(&models.Suggestion{}).
		Where("id = ?", req.ID).
		Updates(map[string]interface{}{
			"reply":  req.Reply,
			"status": "已处理",
		}).Error; err != nil {
		utils.Error(c, "回复失败")
		return
	}

	utils.Success(c, nil, "回复成功")
}

// UpdateStatus 更新状态
func (ctrl *SuggestionController) UpdateStatus(c *gin.Context) {
	var req struct {
		ID     uint   `json:"id" binding:"required"`
		Status string `json:"status" binding:"required"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		utils.Error(c, "参数错误")
		return
	}

	if err := config.DB.Model(&models.Suggestion{}).
		Where("id = ?", req.ID).
		Update("status", req.Status).Error; err != nil {
		utils.Error(c, "更新失败")
		return
	}

	utils.Success(c, nil, "更新成功")
}

// Delete 删除建议
func (ctrl *SuggestionController) Delete(c *gin.Context) {
	var req struct {
		ID uint `json:"id" binding:"required"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		utils.Error(c, "参数错误")
		return
	}

	if err := config.DB.Delete(&models.Suggestion{}, req.ID).Error; err != nil {
		utils.Error(c, "删除失败")
		return
	}

	utils.Success(c, nil, "删除成功")
}

// Statistics 统计
func (ctrl *SuggestionController) Statistics(c *gin.Context) {
	var totalCount int64
	var pendingCount int64
	var processedCount int64

	config.DB.Model(&models.Suggestion{}).Count(&totalCount)
	config.DB.Model(&models.Suggestion{}).Where("status = ?", "待处理").Count(&pendingCount)
	config.DB.Model(&models.Suggestion{}).Where("status = ?", "已处理").Count(&processedCount)

	utils.Success(c, gin.H{
		"total_count":     totalCount,
		"pending_count":   pendingCount,
		"processed_count": processedCount,
	})
}
